package com.hunttown.mes.manage.controller._basic;

import com.hunttown.mes.manage.controller.common.ModelService;
import com.hunttown.mes.manage.service.TransportUserCardLogManageService;
import com.hunttown.mes.rpc.domain.TransportUserCardLogDTO;
import com.hunttown.mes.rpc.domain.enums.pay.PayStatusEnum;
import com.hunttown.mes.rpc.domain.enums.pay.ReceiveWayEnum;
import com.hunttown.mes.rpc.domain.enums.pay.RechargeTypeEnum;
import com.hunttown.mes.rpc.domain.query.TransportUserCardLogDTOQuery;
import com.hunttown.common.domain.Page;
import com.hunttown.mes.common.utils.DateConvertUtils;
import com.hunttown.mes.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.math.BigDecimal;

import static com.hunttown.mes.common.utils.PageUtils.PagePackage;

/**
 * @title: 会员卡日志
 * @author: wangjunfu
 * @date: 2020-06-06 11:20:51
 * @description:
 */
@Controller
@RequestMapping(value = "/basic/paylog")
public class UserCardLogController {

    private final static Logger logger = LoggerFactory.getLogger(UserCardLogController.class);

    private final TransportUserCardLogManageService cardLogService;
    private final ModelService modelService;

    @Autowired
    public UserCardLogController(TransportUserCardLogManageService cardLogService, ModelService modelService) {
        this.cardLogService = cardLogService;
        this.modelService = modelService;
    }

    //region 数据列表

    /**
     * 支付日志-首页
     *
     * @param model    model模型
     * @param dtoQuery 查询条件
     * @param templete 模板
     * @return vm
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model, TransportUserCardLogDTOQuery dtoQuery, String templete) {

        //日期（初始化 2天）
        if (StringUtils.isBlank(dtoQuery.getDaterange())) {
            dtoQuery.setDaterange(String.format("%s ~ %s", DateConvertUtils.getPastDate(0), DateConvertUtils.getPastDate(0)));
        }

        dtoQuery.setUserId(modelService.completionUserId(dtoQuery.getUserId(), dtoQuery.getTrueName(), -9999));
        dtoQuery.setOrderBy("order by id desc");
        Page<TransportUserCardLogDTO> dataPage = cardLogService.getForPage(dtoQuery);

        BigDecimal totalFee = new BigDecimal(0);
        for (TransportUserCardLogDTO item : dataPage.getItems()) {
            item.setTrueName(modelService.getUserTrueName(item.getUserId()));
            item.setStatusName(PayStatusEnum.getEnumByKey(item.getStatus()));
            item.setRechargeTypeName(RechargeTypeEnum.getEnumByKey(item.getRechargeType()));
            item.setPayCompayName(ReceiveWayEnum.getEnumByKey(item.getPayCompay() == null ? -1 : item.getPayCompay()));

            //充值加，扣费减，统计数据不计
            if (item.getRechargeType() != RechargeTypeEnum.STATISTIC.getKey()) {
                if (item.getRechargeType() == RechargeTypeEnum.RECHARGE.getKey()) {
                    totalFee = totalFee.add(item.getRechargeAmount());
                } else {
                    totalFee = totalFee.subtract(item.getRechargeAmount());
                }
            }
        }

        model.addAttribute("page", dataPage);
        model.addAttribute("PagePackage", PagePackage(dataPage.getPn(), dataPage.getStart(), dataPage.getPageSize(), dataPage.getTotalPages(), dataPage.getTotalItems()));
        model.addAttribute("dtoQuery", dtoQuery);
        model.addAttribute("totalFee", totalFee);
        model.addAttribute("templete", templete);

        //日期
        if (StringUtils.isNotBlank(dtoQuery.getDaterange())) {
            String[] date = dtoQuery.getDaterange().split("~");
            if (date.length == 2) {
                model.addAttribute("startDate", date[0].trim());
                model.addAttribute("endDate", date[1].trim());
            }
        }

        //支付状态
        model.addAttribute("PayStatusList", PayStatusEnum.getEnumList());

        if (StringUtils.isBlank(templete)) {
            return "basic/paylog/index";
        } else {
            return "basic/paylog/" + templete;
        }
    }
    //endregion

    //region 数据查看

    /**
     * 数据查看
     *
     * @param model     model模型
     * @param id        主键id
     * @param tableName 数据库英文表名
     * @return vm
     */
    @RequestMapping(value = "/show", method = RequestMethod.POST)
    public String show(Model model, Integer id, String tableName) {
        id = (id == null ? 0 : id);

        TransportUserCardLogDTO obj = new TransportUserCardLogDTO();
        obj.setId(id);

        if (id > 0) {
            obj = cardLogService.getById(id);
        }
        cardLogService.assembleObjInfo(model, tableName, obj);

        return "show/show-template";
    }

    //endregion

}